package visualization;

import gov.nasa.worldwind.Configuration;
import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.event.PositionEvent;
import gov.nasa.worldwind.event.PositionListener;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.*;
import gov.nasa.worldwind.render.Polygon;
import gov.nasa.worldwind.view.orbit.BasicOrbitView;
import gov.nasa.worldwindx.examples.ApplicationTemplate;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import oglTest.oglTest;

/**
 *
 * @author Dark Mark
 */
//public class Main extends ApplicationTemplate
public class Main extends AppTemplate
{
    protected static final double TRACK_LATITUDE = 54.715;
    protected static final double TRACK_LONGITUDE = 18.881;
    static double minDistance = 5000, maxDistance = 8000;
    static double altitude = maxDistance + 1;

    //public static class AppFrame extends ApplicationTemplate.AppFrame
    public static class AppFrame extends AppTemplate.AppFrameZ
    {
        protected gov.nasa.worldwind.layers.Layer renderableLayer;

        public AppFrame()
        {
            super(true, true, false);

            //wywwietlenie ramki otwierajacej
            loadingFrame = new LoadingFrame();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            loadingFrame.setLocation(screenSize.width / 2 - 250, screenSize.height / 2 - 150);
            loadingFrame.setVisible(true);

            if (!lastFilePath.isEmpty())
            {
                //utworzenie wc cache
                loadingFrame.setTitle("Water Column Cache ...");
                String[] cmd =
                {
                    "wcCache.exe"
                };
                Process p;
                try
                {
                    p = Runtime.getRuntime().exec(cmd);
                    p.waitFor();
                }
                catch (Exception ex)
                {
                }
            }

            loadingFrame.setTitle("Depth Data ...");

            //readFile();
            // dodanie warstwy z modelem glebokosciowym
            //replaceMarkerLayer();

            // Add renderable layer for legend
            this.renderableLayer = new RenderableLayer();
            this.renderableLayer.setName("Markers Legend");
            this.renderableLayer.setPickEnabled(false);
            insertBeforePlacenames((gov.nasa.worldwind.WorldWindow) this.getWwd(), this.renderableLayer);

            // Update layer panel
            this.getLayerPanel().update((gov.nasa.worldwind.WorldWindow) this.getWwd());

            // stworzenie kamery z wylaczonym sprawdzaniem kolizji
            //BasicOrbitView bview = (BasicOrbitView) this.getWwd().getView();
            SuperView view = new SuperView((BasicOrbitView) this.getWwd().getView());
            view.setDetectCollisions(false);
            this.getWwd().setView(view);
            //System.out.println(view.getNearClipDistance());
            //ViewInputHandler vih =  view.getViewInputHandler();

            // aktualizacja widoku przy poruszeniu kamera
            /*
            this.getWwd().addPositionListener(new PositionListener()
            {
            @Override
            public void moved(PositionEvent pe)
            {
            //altitude = pe.getPosition().getAltitude();
            altitude = getWwd().getView().getEyePosition().getAltitude();
            //System.out.println(altitude);
            //System.out.println(getWwd().getView().getEyePosition().latitude + ", " + getWwd().getView().getEyePosition().longitude);

            // ustawienie przezroczystosci warstw
            if (altitude <= maxDistance + minDistance)
            {
            getWwd().getModel().getLayers().get(3).setOpacity((altitude - minDistance) / maxDistance);
            getWwd().getModel().getLayers().get(4).setOpacity((altitude - minDistance) / maxDistance);
            getWwd().getModel().getLayers().get(1).setEnabled(false);
            }
            else
            {
            getWwd().getModel().getLayers().get(3).setOpacity(1.0);
            getWwd().getModel().getLayers().get(4).setOpacity(1.0);
            getWwd().getModel().getLayers().get(1).setEnabled(true);
            }
            for(Layer l : getWwd().getModel().getLayers())
            {
            if(l.getName().equals("i-cubed Landsat") || l.getName().equals("Blue Marble (WMS) 2004"))
            {
            //l.setEnabled(false);//wylaczenie niepotrzebnych warstw
            }
            }
            }
            });
             */

            //System.out.println(getWwd().getModel().getLayers().get(4).getName());

            //this.getWwd().setVisible(false);

            //LogicThread lThread = new LogicThread(this);
            //lThread.start();

            // dodanie warstwy ze statkiem (sonarem)
            RenderableLayer shipLayer = new RenderableLayer();
            shipLayer.setName("Ship");
            ArrayList<Position> shipBorders = new ArrayList<Position>();
            shipBorders.add(Position.fromDegrees(0, 0, 10e4));
            shipBorders.add(Position.fromDegrees(0, 0.0005, 10e4));
            shipBorders.add(Position.fromDegrees(0.0005, 0.0005, 10e4));
            shipBorders.add(Position.fromDegrees(0.0005, 0, 10e4));
            //ship = new Polygon(shipBorders);
            //ship = new Ship(shipBorders);
            ship = new Ship(0.0001f, 0.0001f, 60f);
            //ship.setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);
            shipLayer.addRenderable(ship);
            //insertBeforeCompass(getWwd(), shipLayer);
            //this.getLayerPanel().update(this.getWwd());
            replaceLayer(shipLayer);

            replaceMarkerLayer();

            loadingFrame.setVisible(false);
            loadingFrame.dispose();
        }
    }

    public static void main(String[] args)
    {
        Configuration.setValue(AVKey.INITIAL_LATITUDE, TRACK_LATITUDE);
        Configuration.setValue(AVKey.INITIAL_LONGITUDE, TRACK_LONGITUDE);
        //Configuration.setValue(AVKey.INITIAL_ALTITUDE, 40e3);
        Configuration.setValue(AVKey.INITIAL_ALTITUDE, 100000);
        ApplicationTemplate.start("World Wind Sonars", AppFrame.class);

        //AppFrame appFrame = new Main.AppFrame();
        //ApplicationTemplate.start("World Wind Sonars", Main.AppFrame.class);

        //LogicThread lThread = new LogicThread();
        //lThread.start();
    }
}
